﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class CheckTypeDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(CheckType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CheckType(");
            strSql.Append("CarInfoId,BaseItemTypeId,Appearance,Gps,HiddenRectification,CreateTime,UserId)");
            strSql.Append(" values (");
            strSql.Append("@CarInfoId,@BaseItemTypeId,@Appearance,@Gps,@HiddenRectification,@CreateTime,@UserId)");
            SqlParameter[] parameters = {
					new SqlParameter("@CarInfoId", SqlDbType.Int,4),
					new SqlParameter("@BaseItemTypeId", SqlDbType.Int,4),
					new SqlParameter("@Appearance", SqlDbType.NVarChar,50),
					new SqlParameter("@Gps", SqlDbType.NVarChar,50),
					new SqlParameter("@HiddenRectification", SqlDbType.NVarChar,500),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UserId", SqlDbType.Int,4)};
            parameters[0].Value = model.CarInfoId;
            parameters[1].Value = model.BaseItemTypeId;
            parameters[2].Value = model.Appearance;
            parameters[3].Value = model.Gps;
            parameters[4].Value = model.HiddenRectification;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.UserId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CheckType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CheckType set ");
            strSql.Append("CarInfoId=@CarInfoId,");
            strSql.Append("BaseItemTypeId=@BaseItemTypeId,");
            strSql.Append("Appearance=@Appearance,");
            strSql.Append("Gps=@Gps,");
            strSql.Append("HiddenRectification=@HiddenRectification");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@CarInfoId", SqlDbType.Int,4),
					new SqlParameter("@BaseItemTypeId", SqlDbType.Int,4),
					new SqlParameter("@Appearance", SqlDbType.NVarChar,50),
					new SqlParameter("@Gps", SqlDbType.NVarChar,50),
					new SqlParameter("@HiddenRectification", SqlDbType.NVarChar,500),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.CarInfoId;
            parameters[1].Value = model.BaseItemTypeId;
            parameters[2].Value = model.Appearance;
            parameters[3].Value = model.Gps;
            parameters[4].Value = model.HiddenRectification;
            parameters[5].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CheckType ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CheckType ");
            strSql.Append(" where Id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获取单个模型
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public CheckType GetModel(int id)
        {
            string sql = "SELECT * FROM CheckType WHERE Id=@Id";
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@Id",SqlDbType.Int)
            };
            CheckType obj = null;
            param[0].Value = id;
            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(sql,param))
            {
                if (dr.Read())
                {
                    obj = BuilderModel(dr);
                }
                dr.Close();
            }
            return obj;
        }
        /// <summary>
        /// 根据条件获取前几条数据
        /// </summary>
        /// <param name="strWhere">The Query String.</param>
        /// <returns>CheckType List</returns>
        public IList<CheckType> GetList(string strWhere)
        {
            IList<CheckType> result = new List<CheckType>();
            string sql = "SELECT * FROM CheckType WHERE 1=1 " + strWhere;

            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(sql))
            {
                while (dr.Read())
                {
                    result.Add(BuilderModel(dr));
                }
                dr.Close();
            }
            return result;
        }
        /// <summary>
        /// 组装代码
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public CheckType BuilderModel(IDataReader dr)
        {
            try
            {
                CheckType obj = new CheckType();
                obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                obj.CarInfoId = (dr["CarInfoId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("CarInfoId")));
                obj.BaseItemTypeId = (dr["BaseItemTypeId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("BaseItemTypeId")));
                obj.Appearance = (dr["Appearance"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("Appearance")));
                obj.Gps = (dr["Gps"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("Gps")));
                obj.HiddenRectification = (dr["HiddenRectification"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("HiddenRectification")));
                obj.CreateTime = (dr["CreateTime"] == System.DBNull.Value ? new DateTime(1900, 1, 1) : dr.GetDateTime(dr.GetOrdinal("CreateTime")));
                obj.UserId = (dr["UserId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("UserId")));
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
            }
        }
        #endregion  BasicMethod
    }
}
